package com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/*
 * 目录外药品预订单CMD
 */
@Controller
public class OffCatalogDrugReservationCmd {
	
	/*
	 * 加载药品已接单订单的所有订单
	 */
	@Mapping("com.awspaass.user.apps.OffCatalogDrugReservationCheck")
	public static String getOffCatalogDrugReservationCheck(UserContext uc,String zgname,String drugname) {
		JSONObject result = new JSONObject();
		String checksql="";
		String sql="";
		if(!"".equals(zgname)) {//职工姓名
			sql = " and t.WORKERNAME >='" + zgname + "' ";
			checksql +=sql;
		}
		if(!"".equals(drugname)) { //药品名称
			sql =" and t.DRUGNAME like '%" + drugname + "%' ";
			checksql +=sql;
		}
		try {
			if(!"".equals(checksql)) {
				 String querySql ="select t.ID,t.BINDID,t.WORKERUNIT,t.WORKERNAME,t.PHONE,t.DRUGNAME,t.PROCURENUM,t.WGDNAME,t.STATE "
				 		+ ", t.WORKERID from  BO_EU_DIRECTORYOUT_DRUGDUE  t  where t.STATE in  ('2') "+ checksql+ "  order by  t.createdate  asc";
				 List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
					if (urlList == null || urlList.isEmpty()) {
						JSONArray itemArray = new JSONArray();
						result.put("status", "0");
						result.put("item", itemArray);
					}
					JSONArray itemArray = new JSONArray();
					for (int i = 0; i < urlList.size(); i++) {
						Map<String, Object> urlMap = urlList.get(i);
						JSONObject item = new JSONObject();
						String ID = CoreUtil.objToStr(urlMap.get("ID"));// ID
						String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// BINDID
						String WORKERUNIT = CoreUtil.objToStr(urlMap.get("WORKERUNIT"));// 职工单位
						String WORKERNAME = CoreUtil.objToStr(urlMap.get("WORKERNAME"));// 职工姓名
						String PHONE = CoreUtil.objToStr(urlMap.get("PHONE"));// 联系电话
						String DRUGNAME = CoreUtil.objToStr(urlMap.get("DRUGNAME"));// 药品名称
						String PROCURENUM = CoreUtil.objToStr(urlMap.get("PROCURENUM"));// 采购数量
						String WGDNAME = CoreUtil.objToStr(urlMap.get("WGDNAME"));// 取药地点
						String STATE = CoreUtil.objToStr(urlMap.get("STATE"));// 状态
						String WORKERID = CoreUtil.objToStr(urlMap.get("WORKERID"));// 职工账号
						item.put("zbid", ID);	
						item.put("bindid", BINDID);
						item.put("workerunit", WORKERUNIT);
						item.put("workername", WORKERNAME);
						item.put("phone", PHONE);
						item.put("drugname", DRUGNAME);
						item.put("procurenum", PROCURENUM);
						item.put("wgdname", WGDNAME);
						item.put("state", STATE);
						item.put("workerid", WORKERID);
						itemArray.add(item);
					 }
					result.put("status", "0");
					result.put("item", itemArray);
				    
			}else {
				String querySql ="select t.ID,t.BINDID,t.WORKERUNIT,t.WORKERNAME,t.PHONE,t.DRUGNAME,t.PROCURENUM,t.WGDNAME,t.STATE "
				 		+ ", t.WORKERID from  BO_EU_DIRECTORYOUT_DRUGDUE  t  where t.STATE in  ('2')  order by  t.createdate  asc";
				 List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
					if (urlList == null || urlList.isEmpty()) {
						JSONArray itemArray = new JSONArray();
						result.put("status", "0");
						result.put("item", itemArray);
					}
					JSONArray itemArray = new JSONArray();
					for (int i = 0; i < urlList.size(); i++) {
						Map<String, Object> urlMap = urlList.get(i);
						JSONObject item = new JSONObject();
						String ID = CoreUtil.objToStr(urlMap.get("ID"));// ID
						String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// BINDID
						String WORKERUNIT = CoreUtil.objToStr(urlMap.get("WORKERUNIT"));// 职工单位
						String WORKERNAME = CoreUtil.objToStr(urlMap.get("WORKERNAME"));// 职工姓名
						String PHONE = CoreUtil.objToStr(urlMap.get("PHONE"));// 联系电话
						String DRUGNAME = CoreUtil.objToStr(urlMap.get("DRUGNAME"));// 药品名称
						String PROCURENUM = CoreUtil.objToStr(urlMap.get("PROCURENUM"));// 采购数量
						String WGDNAME = CoreUtil.objToStr(urlMap.get("WGDNAME"));// 取药地点
						String STATE = CoreUtil.objToStr(urlMap.get("STATE"));// 状态
						String WORKERID = CoreUtil.objToStr(urlMap.get("WORKERID"));// 职工账号
						item.put("zbid", ID);	
						item.put("bindid", BINDID);
						item.put("workerunit", WORKERUNIT);
						item.put("workername", WORKERNAME);
						item.put("phone", PHONE);
						item.put("drugname", DRUGNAME);
						item.put("procurenum", PROCURENUM);
						item.put("wgdname", WGDNAME);
						item.put("state", STATE);
						item.put("workerid", WORKERID);
						itemArray.add(item);
					 
				 }
				result.put("status", "0");
				result.put("item", itemArray);
		  }
		} catch (Exception e) {

			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}
	/*
	 * 健康管理中心客服发送已接单订单药品采购完成
	 * 0:未提交|1:已提交|2:已接单|3:采购完成|4:退回
	 */
	@Mapping("com.awspaass.user.apps.OffCatalogDrugReservationSendmessage")
	public String sendOffCatalogDrugReservation(String id ,String workerid ,String workerunit ,String drugname,String wgdname,UserContext uc) throws SQLException {
		JSONObject returnData = new JSONObject();
		String TELEPHONE =uc.getUserModel().getMobile();//节点办理人电话
		String GUANLIYUAN =uc.getUserModel().getUserName();//办理人名称
		if((null == id || "".equals(id)) && (null == workerid || "".equals(workerid))
				&& (null == workerunit || "".equals(workerunit))) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		//更新状态
		try {
			String updatenosql = "update  BO_EU_DIRECTORYOUT_DRUGDUE set STATE ='3'  where id ='"
					+ id + "'";
			DBSql.update(updatenosql);
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();

		}
		//发送企业微信消息
		String querySql ="select b.PHONE from BO_EU_DIRECTORYOUT_DRUGDUE t " + 
				"left join BO_EU_GRIDPOINT b on  t.wgdid =b.id where t.id ='"+id+"'";
		String phone =CoreUtil.objToStr(DBSql.getString(querySql,"PHONE"));
		 String sqluserId ="select EXT5  from ORGUSER  where USERID='"+workerid+"'";
         String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
         String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+workerunit+"'";
        Connection conn = null;
 		Statement stat = null;
 		ResultSet rs = null;
 		conn = DBSql.open();
 		stat = conn.createStatement();
 		rs = stat.executeQuery(sql);
 		if (rs != null && !"".equals(weixinid)) {
			while (rs.next()) {
				String content="";
				if(!"".equals(phone)) {
					content ="您好！您预订的"+drugname+"药品已到，请至"+wgdname+"服务点就诊开药,联系电话:"+phone+"。";
				}else {
					content ="您好！您预订的"+drugname+"药品已到，请至"+wgdname+"服务点就诊开药。";
				}
				
				String agentid = CoreUtil.objToStr(rs.getObject("AGENTID"));//应用id
				String corpid = CoreUtil.objToStr(rs.getObject("CORPID"));// 企业微信单位id
				String agentsecretid = CoreUtil.objToStr(rs.getObject("AGENTSECRETID"));//应用secretid
				String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
				weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",content,0,agentid,acessToken);
				System.out.println("健康管理中心客服发送已接单订单药品采购完成消息发送成功！");
			}
 		}
 		 DBSql.close(conn, stat, rs);
         return returnData.toString();

	}
}
